package com.itheima.service.imp;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.constant.MessageConstant;
import com.itheima.dao.CheckGroupDao;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.CheckGroup;
import com.itheima.service.CheckGroupService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;

@Service(interfaceClass = CheckGroupService.class)
@Transactional
public class CheckGroupServiceImp implements CheckGroupService {
    @Autowired
    private CheckGroupDao checkGroupDao;
    @Override
    @Transactional(rollbackFor=Exception.class,propagation = Propagation.REQUIRES_NEW)
    public Result add(int[] checkItemIds, CheckGroup checkGroup) {
        System.out.println(checkItemIds);
        int i = checkGroupDao.add(checkGroup);

        System.out.println(checkGroup.getId());

        if(checkItemIds!=null&&checkItemIds.length!=0){
            for (int checkItemId : checkItemIds) {
                System.out.println(checkItemId);
                checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(),checkItemId);
            }
        }


        return  new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageHelper.startPage(queryPageBean.getCurrentPage(),queryPageBean.getPageSize());
        if(queryPageBean.getQueryString()==null){
            queryPageBean.setQueryString("%%");
        }else{
            queryPageBean.setQueryString("%"+queryPageBean.getQueryString()+"%");
        }

        Page<CheckGroup> checkGroupList = checkGroupDao.findPage(queryPageBean);

        return new PageResult(checkGroupList.getTotal(),checkGroupList.getResult());
    }

    @Override
    public Result delete(int id) {
        checkGroupDao.deleteCentre(id);
        checkGroupDao.deleteById(id);

        return new Result(true,MessageConstant.DELETE_CHECKGROUP_SUCCESS);
    }

    @Override
    public Result findItemIdsByGroupId(int id) {

        List<Integer> idS = checkGroupDao.findItemIdsByGroupId(id);
        System.out.println(idS);
        CheckGroup checkGroup = checkGroupDao.findcheckGroupById(id);
        HashMap<String, Object> map = new HashMap<>();

        map.put("itemIds",idS);
        map.put("checkGroup",checkGroup);
        return new Result(true,null,map);
    }

    @Override
    public Result edit(int[] checkitemIds, CheckGroup checkGroup) {


        List<Integer> itemIdsByGroupId = checkGroupDao.findItemIdsByGroupId(checkGroup.getId());
        checkGroupDao.deleteCentre(checkGroup.getId());

        for (int checkitemId : checkitemIds) {
            checkGroupDao.addCheckGroupAndCheckItem(checkGroup.getId(),checkitemId);
        }
        //修改表单数据
        checkGroupDao.update(checkGroup);

      return new Result(true,null);
    }
    //查询所有
    public Result findAll(){
        List<CheckGroup> checkGroupList = checkGroupDao.findAll();
        return new Result(true,null,checkGroupList);
    }
}
